package com.wl.exception;

import com.wl.resultcore.ProjectConstant;
import com.wl.resultcore.Result;
import com.wl.resultcore.ResultGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.expression.AccessException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;


import javax.servlet.http.HttpServletResponse;
import java.sql.SQLSyntaxErrorException;

/**
 * 自定义全局异常处理器
 */
@RestControllerAdvice
public class GlobalDefaultExceptionHandler {

	private static final Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);


	/**
	 * 处理 AccessException 类型的异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(AccessException.class)
	public Result accessExceptionHandler(Exception e) {
		e.printStackTrace();
		logger.error("处理 AccessException 类型的异常:" + e.getMessage());
		return ResultGenerator.genFailCodeResult(ProjectConstant.ACCESS_INVALID,e.getMessage());
	}

	/**
	 * 处理 MaxUploadSizeExceededException 上传文件过大 类型的异常
	 */
//	@ExceptionHandler(MaxUploadSizeExceededException.class)
//	public Result defaultMaxUploadSizeExceededException(Exception e, HttpServletResponse response){
//		e.printStackTrace();
//		logger.error("处理 MaxUploadSizeExceededException 类型的异常:" + e.getMessage());
//		return ResultGenerator.genFailCodeResult(ProjectConstant.IMPORT_FAIL,"上传文件不能超过"+maxFile);
//	}

	/**
	 * 处理 Exception 类型的异常
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	public Result defaultExceptionHandler(Exception e) {
		e.printStackTrace();
		logger.error("处理"+ e +"类型的异常:" + e.getMessage());
		return ResultGenerator.genFailCodeResult(ProjectConstant.SERVICE_INVALID,e.getMessage());
	}

	/**
	 * 处理 Service RuntimeException 类型的异常
	 */
	@ExceptionHandler(RuntimeException.class)
	public Result defaultServiceExceptionHandler(Exception e){
		e.printStackTrace();
		logger.error("处理"+ e +"类型的异常:" + e.getMessage());
		return ResultGenerator.genFailCodeResult(ProjectConstant.SERVICE_INVALID,e.getMessage());
	}

	/**
	 * 处理 NoHandlerFoundException 类型的异常
	 */
	@ExceptionHandler(NoHandlerFoundException.class)
	public void defaultNoHandlerFoundExceptionHandler(Exception e, HttpServletResponse response){
		e.printStackTrace();
		logger.error("处理 NoHandlerFoundException 类型的异常:" + e.getMessage());
		response.setStatus(404);
	}

	/**
	 * 处理 SQLSyntaxErrorException 类型的异常
	 */
	@ExceptionHandler(SQLSyntaxErrorException.class)
	public Result defaultSQLSyntaxErrorExceptionHandler(Exception e, HttpServletResponse response){
		e.printStackTrace();
		logger.error("处理 SQLSyntaxErrorException 类型的异常:" + e.getMessage());
		return ResultGenerator.genFailResult("参数异常，查询失败!");
	}

	/**
	 * 处理 BadSqlGrammarException 类型的异常
	 */
	@ExceptionHandler(BadSqlGrammarException.class)
	public Result defaultBadSqlGrammarExceptionHandler(Exception e, HttpServletResponse response){
		e.printStackTrace();
		logger.error("处理 BadSqlGrammarException 类型的异常:" + e.getMessage());
		return ResultGenerator.genFailResult("参数异常，查询失败!");
	}


}
